package 链表;

import java.util.Stack;

/**
 * @Author：小鹿爱吃鱼
 * @Date: 2025/5/6 19:38
 * @Description:
 */
public class 回文链表 {

    public boolean isPalindrome(ListNode head) {
        Stack<ListNode> stack = new Stack<>();
        ListNode currentNode = head;
        while (currentNode != null) {
            stack.push(currentNode);
            currentNode = currentNode.next;
        }

        currentNode = head;
        while (!stack.isEmpty()) {
            if(stack.pop().val != currentNode.val){  // 一定要判断val 一定要判断val
                return false;
            }else{
                currentNode = currentNode.next;
            }
        }
        return true;
    }


    public static void main(String[] args) {

    }

    static class ListNode{
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }



}





/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */